Python sensor creation oddities

HI,

i have this little python code section the sets the feedback i get for my alarmclock in 2 ways: a text bit, and a sensor with a badge:

45
27

All seems fine, until I check the state.state of the sensor, and its showing On Off, in its variations. Apparently it shows both the weekdays and weekends on/off setting. How could I correct this in the python (see below) so that it shows only 1 state…

relevant creation line is

hass.states.set('sensor.alarms_badge','', {

where ive taken out the state.state now with this effect:

55 (only weekdays showing)

using:

hass.states.set(‘sensor.alarms_badge’,state.state, {

shows on Off:

19

53 WE off Wd Off

also, why the wD state is shown with a capital O and the wE state without is unclear to me, since i don’t set it manually. Or would that be because it currently is a weekday?

##########################################################################################
## Alarm clock
## https://github.com/maattdiy/home-assistant-config/blob/master/config/packages/alarmclock.yaml
##########################################################################################

alarms_prefix = ['alarmclock_wd', 'alarmclock_we']
alarms_wfilter = ['1|2|3|4|5', '6|7']
alarms_desc = ''
idx = 0
alarms_uom = []
alarms_picture = []
alarms_fn = []

for entity_id in alarms_prefix:
    state = hass.states.get('input_boolean.{}_enabled'.format(entity_id))
    if state:
        if state.state is 'on':
            # Show the alarm for the next day
            if (str(datetime.datetime.now().isoweekday()) in alarms_wfilter[idx].split('|')):
                state = hass.states.get('sensor.{}_time_template'.format(entity_id))
                alarms_desc = '{}{}, '.format(alarms_desc, state.state)
    idx = idx + 1

if (alarms_desc == ''):
    alarms_fn = 'Relax'
    alarms_desc = '%- Alarm clock is not set, relax'
    alarms_uom = 'Off'
    alarms_picture = '/local/badges/alarm_off.png'
else:
    alarms_fn = alarms_desc[:-2]
    alarms_desc = '/- Alarm clock set at ' + alarms_desc[:-2]
    alarms_uom = 'On'
    alarms_picture = '/local/badges/alarm.png'

hass.states.set('sensor.alarms_badge','', {
         'entity_picture': alarms_picture,
         'friendly_name': alarms_fn,
         'unit_of_measurement': alarms_uom,
         'hidden': hidden,
         'order': order
          })

4706

refactored it a bit, same logic, easier to follow:

##########################################################################################
## Alarm clock
## https://github.com/maattdiy/home-assistant-config/blob/master/config/packages/alarmclock.yaml
##########################################################################################

from datetime import datetime

alarms_prefix = ['alarmclock_wd', 'alarmclock_we']
alarms_wfilter = [range(1,6), range(6,8)]
alarms_desc = ''
mydict = {'hidden':hidden, 'order':order}

for entity_id, filter in zip(alarms_prefix, alarms_wfilter):
    state = hass.states.get('input_boolean.{}_enabled'.format(entity_id))
    if state:
        if state.state is 'on' and datetime.now().isoweekday() in filter:
            state = hass.states.get('sensor.{}_time_template'.format(entity_id))
            alarms_desc = '{}{}, '.format(alarms_desc, state.state)

if (alarms_desc == ''):
    mydict['entity_picture'] = '/local/badges/alarm_off.png'
    mydict['friendly_name'] =  'Relax'
    mydict['unit_of_measurement'] = 'Off'
    alarms_desc = '%- Alarm clock is not set, relax'
else:
    mydict['entity_picture'] = '/local/badges/alarm.png'
    mydict['friendly_name'] = alarms_desc[:-2]
    mydict['unit_of_measurement'] = 'On'
    alarms_desc = '/- Alarm clock set at ' + alarms_desc[:-2]

hass.states.set('sensor.alarms_badge', '', my_dict)

It’s not showing both values. This is because you are setting the unit of measure to on. Normally, the sensor object would have a number in the circle instead of an image. Then the units are displayed in that nice little box at the bottom of the round sensor object. If you remove mydict[‘unit_of_measurement’], you will not have the on On, off Off. Normal sensors would be a temperature sensor. If the temp was 25, and the unit_of_measurement was ºF, then the state would be 25 ºF. Your state is on, and your unit_of_measurement is On, so your state is on On.

1 Like

wow, thanks!

trying to understand all of this, and hope to translate that to my bigger script… might need your help again soon :wink: magic how you do this, im trying to learn here, but this is kind of on the horizon of my imagination…

i do get this import not found error though, so no fun yet:

btw, noticed today, and check the top image that the alarm is off, and the time-wd icon still showing the clock wile it should have have shown mdi:close-circle…

see:

sensor:
  - platform: template
    sensors:
      alarmclock_wd_time_template:
        friendly_name: 'Time wd'
        icon_template: "{{ 'mdi:alarm' if is_state('input_boolean.alarmclock_wd_enabled', 'on') else 'mdi:close-circle' }}"
        value_template: '{{ "%0.02d:%0.02d" | format(states("input_number.alarmclock_wd_hour") | int, states("input_number.alarmclock_wd_minute") | int) }}'

I’ve checked the state in the dev tools, and that is correct showing the icon name:

what could be up?

you may not be able to from datetime import datetime in the spot I put it.

Might need to do this:

##########################################################################################
## Alarm clock
## https://github.com/maattdiy/home-assistant-config/blob/master/config/packages/alarmclock.yaml
##########################################################################################

alarms_prefix = ['alarmclock_wd', 'alarmclock_we']
alarms_wfilter = [range(1,6), range(6,8)]
alarms_desc = ''
mydict = {'hidden':hidden, 'order':order}

for entity_id, filter in zip(alarms_prefix, alarms_wfilter):
    state = hass.states.get('input_boolean.{}_enabled'.format(entity_id))
    if state:
        if state.state is 'on' and datetime.datetime.now().isoweekday() in filter:
            state = hass.states.get('sensor.{}_time_template'.format(entity_id))
            alarms_desc = '{}{}, '.format(alarms_desc, state.state)

if (alarms_desc == ''):
    mydict['entity_picture'] = '/local/badges/alarm_off.png'
    mydict['friendly_name'] =  'Relax'
    mydict['unit_of_measurement'] = 'Off'
    alarms_desc = '%- Alarm clock is not set, relax'
else:
    mydict['entity_picture'] = '/local/badges/alarm.png'
    mydict['friendly_name'] = alarms_desc[:-2]
    mydict['unit_of_measurement'] = 'On'
    alarms_desc = '/- Alarm clock set at ' + alarms_desc[:-2]

hass.states.set('sensor.alarms_badge', '', my_dict)

Not sure, that’s something that I would need to debug into personally.

will do, report back.

btw, what does the alarms_desc[:-2] do with the alarms_desc. never found the docs to learn what it means? if you have a pointer to that id appreciate

if you have a string or list, it allows you to grab portions of it.

Example:

mystring =‘123456789’
foo = mystring[:-2] would return everything before the second to last digit (i == -2, where last item in collection is -1)
foo = mystring[2:] would return everything after the 3rd index (i==2, where i starts at 0)
foo = mystring[2] would return the 3rd index (i==2, where i starts at 0)

This is all standard list/string manipulation. Just about every software language uses something like this.

bingo!
had to change this too (found it after checking ten times…)
hass.states.set('sensor.alarms_badge', '', mydict)

thanks! bit puzzled by the range setting though, seems they overlap now (6)?

index’s in lists are off by 1

So a range of 6, range(6) returns [ 0,1,2,3,4,5 ]. You only want days 1-5, hence Range(1,6) which returns [ 1,2,3,4,5]