hi,
have a python-script Summary which shows on/oflline status if entities in several groups. Switches, lights, etc.
time of change in these entities is set by last_triggered of an automation checking the state change of these entities.
Upon first run (after a reboot) and when all are off, the script shows the lights just fine (No lights on) but wont load the switches. When any entities are online, they show perfectly.
for as far as my eyes can check, scripts and automations are identical, so I wonder if the entity type has got something to do with the difference.
What could be the reason for this behavioral difference?
As a temporarily fix, I have created an automation that runs the sense_switches_change at startup unconditionally, to set the last_triggered, and that works alright. Still, that should not be necessary of course, and the underlying issue isnāt tackledā¦
Apparently the lights automation gets set at startup and the others donāt. both are set to initial_state: 'on'
.
automation:
- alias: 'Sense Lights change'
id: '1511601488008'
# hide_entity: True
initial_state: 'on'
trigger:
platform: state
entity_id: 'light.dining_corner,light.dining_table_lamp_1,light.dining_table_lamp_2,
light.dining_table_lamp_3,light.kist,light.cabinet,light.home_theater,
light.corridor_cabinet,light.bedside_table,light.lounge_chair_long,
light.drawer,light.gamer_den,light.table,light.bureau,light.inside,
light.outside,light.hue_go_1'
condition: []
action:
- service: python_script.anything_on
- delay: 00:00:02
- service: python_script.summary
# Call Summary after Switches change
- alias: 'Sense Switches change'
id: '1511601488009'
# hide_entity: True
initial_state: 'on'
trigger:
platform: state
entity_id: 'switch.sw_audio_auditorium_template,switch.sw_tv_auditorium_template,
switch.sw_espresso_keuken_template,switch.sw_tv_library_template,
switch.sw_kantoor_template,switch.sw_audio_gym_template,
switch.sw_tester_template,switch.sw_dorm_template'
condition: []
action:
- service: python_script.anything_on
- delay: 00:00:02
- service: python_script.summary
python script relevant bit:
##########################################################################################
# Lights:
# Badges images: /config/www/lights
##########################################################################################
lights_group = 'group.all_lights_only'
#show only lights, not light groups
#excluded = ['light.custom_group_for_group','light.custom_group_for_lights_2']
lights_message = []
sensor_lights_message = []
sensor_lights_desc = []
lights_desc = []
lights_on = []
lights_picture = []
lights_uom = []
total_lights_on = hass.states.get('input_boolean.anything_on').attributes.get('lights_on')
if hass.states.get('automation.sense_lights_change').attributes.get('last_triggered'):
for entity_id in hass.states.get(lights_group).attributes['entity_id']:
dt = hass.states.get('automation.sense_lights_change').attributes.get('last_triggered')
dt = dt + datetime.timedelta(hours=2)
time = '%02d:%02d' % (dt.hour, dt.minute)
if hass.states.get(entity_id).state is 'on': #and entity_id not in excluded
lights_on.append(hass.states.get(entity_id).attributes["friendly_name"])
if len(lights_on) > 0:
lights_picture = '/local/lights/hue_pl.png'
lights_message = ', '.join(lights_on)
sensor_lights_message = 'Lights on: ' +', '.join(lights_on)
lights_desc = '=- Lights on: {} -- {} since {}'.format(total_lights_on,lights_message,time)
lights_uom = 'Lights'
if len(lights_on) == 1:
lights_uom = 'Light'
else:
lights_picture = '/local/lights/bulboff.png'
lights_message= ''
sensor_lights_message= 'No lights on'
lights_desc = '!|-> No lights on since {}'.format(time)
lights_uom = 'Lights'
sensor_lights_desc = '{}'.format(sensor_lights_message)
hass.states.set('sensor.lights_badge', total_lights_on, {
# 'custom_ui_state_card': 'state-card-value_only',
'text': sensor_lights_message,
'unit_of_measurement': lights_uom,
'friendly_name': time,
'entity_picture': lights_picture
})
##########################################################################################
# Switches:
# Badges images: /config/www/buttons
##########################################################################################
switches_group = 'group.iungo_switch_switches_template'
switches_message = []
sensor_switches_message = []
sensor_switches_desc = []
switches_desc = []
switches_on = []
switches_picture = []
switches_uom = []
total_switches_on = hass.states.get('input_boolean.anything_on').attributes.get('switches_on')
if hass.states.get('automation.sense_switches_change').attributes.get('last_triggered'):
for entity_id in hass.states.get(switches_group).attributes['entity_id']:
dt = hass.states.get('automation.sense_switches_change').attributes.get('last_triggered')
dt = dt + datetime.timedelta(hours=2)
time = '%02d:%02d' % (dt.hour, dt.minute)
if hass.states.get(entity_id).state is 'on': # and entity_id not in excluded
switches_on.append(hass.states.get(entity_id).attributes["friendly_name"])
if len(switches_on) > 0:
switches_picture = '/local/buttons/button_power_on.png'
switches_message = ', '.join(switches_on)
sensor_switches_message = 'Switches on: ' +', '.join(switches_on)
switches_desc = '#- Switches on: {} -- {} since {}'.format(total_switches_on,switches_message,time)
switches_uom = 'Switches'
if len(switches_on) == 1:
switches_uom = 'Switch'
else:
switches_picture = '/local/buttons/button_power_off.png'
switches_message= ''
sensor_switches_message= 'No switches on'
switches_desc = '!|-> No switches on since {}'.format(time)
switches_uom = 'Switches'
sensor_switches_desc = '{}'.format(sensor_switches_message)
hass.states.set('sensor.switches_badge', total_switches_on, {
# 'custom_ui_state_card': 'state-card-value_only',
'text': sensor_switches_message,
'unit_of_measurement': switches_uom,
'friendly_name': time,
'entity_picture': switches_picture
})
anything_on script called in the python script above:
for entity_id in lightEntities:
state = hass.states.get(entity_id)
# filter out bulbs not on
if (state.state == 'on'):
if ('tradfri' not in state.name):
lightStatus = 'on'
lightsOn = lightsOn + 1
for entity_id in switchEntities:
state = hass.states.get(entity_id)
if (state.state == 'on'):
lightStatus = 'on'
switchesOn = switchesOn + 1
for entity_id in applianceEntities:
state = hass.states.get(entity_id)
if (state.state == 'on'):
lightStatus = 'on'
appliancesOn = appliancesOn + 1
if lightStatus == 'on':
whichIcon = "mdi:lightbulb-on-outline"
totalOn = switchesOn + lightsOn + appliancesOn
# Return sensor state
hass.states.set('input_boolean.anything_on', lightStatus, {
'friendly_name': 'Anything On?',
'icon': whichIcon,
'lights_on': lightsOn,
'switches_on': switchesOn,
'appliances_on': appliancesOn,
'total_on': totalOn,
'extra_data_template':'{total_on} on'
})