Skip to content

Boolean operators return the wrong type #335

@ccapndave

Description

@ccapndave

Whilst trying to make a battery sensor, I stumbled on some very strange behaviour (I'm doing this in a Jupyter notebook using https://github.com/craigbarratt/hass-pyscript-jupyter).

The script at the top should return an empty list, but instead it returns a list. I explored further and there seems to be something strange about the boolean and operator. Here is what's in my notebook:

import re

state_names = ['sun.sun', 'group.battery_level', 'group.battery_alert', 'input_number.battery_alert_threshold_max']

ignore_sensors = ["sun.sun", "group.battery_level"]
[sensor for sensor in state_names if (re.search(r'_battery(_level)?$', sensor) and (sensor not in ignore_sensors))]
['group.battery_alert', 'input_number.battery_alert_threshold_max']

[re.search('battery', sensor) and sensor not in ignore_sensors for sensor in state_names]
[0, 0, True, True]

type(True and False)
<class 'int'>

type(False)
<class 'bool'>

type(True)
<class 'bool'>

type(True and True)
<class 'bool'>

type(True and True)
<class 'bool'>

bool(0)
False

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions