Hi. Please bear with me as I’m trying to learn how to use variables to reduce the number of seperate scripts and automations that I’m making.
I’m trying to use a single script to toggle either of kid’s internet access. I’ve got it partially working.
Right now when I hit a button on the dash that calls this script it starts a timer based on an input_number
which is exposed as a slider.
Here is my dash card including the button for anyone interested:
square: false
type: grid
cards:
- square: false
type: grid
cards:
- show_name: true
show_icon: true
type: button
tap_action:
action: call-service
service: script.lan_kids_internet_timer_script
data:
option: timer.lan_kid1_s_internet_timer
target: {}
entity: switch.kid1_s_chromebook
show_state: true
icon_height: 40px
name: kid1's Internet
- show_name: true
show_icon: true
type: button
tap_action:
action: call-service
service: script.lan_kids_internet_timer_script
data:
option: timer.lan_kid2_s_internet_timer
target: {}
entity: switch.kid2_s_chromebook
show_state: true
icon_height: 40px
name: kid2's Internet
columns: 2
- square: false
type: grid
cards:
- type: custom:mushroom-template-card
primary: >-
{% set t = 'timer.lan_kid1_s_internet_timer' %}
{% set state = states(t) %}
{% set finish = state_attr(t, 'finishes_at') %}
{% set remain = state_attr(t, 'remaining') %}
{% set seconds = (as_timestamp(finish,0)-as_timestamp(now()))|int %}
{% set left = iif(finish == None,iif(remain == None,0,as_timedelta('0
' ~remain).seconds),seconds) %}
{{ left| timestamp_custom('%H:%M:%S', 0) }}
secondary: Time Remaining
icon: mdi:web
tap_action:
action: toggle
hold_action:
action: more-info
icon_color: ''
entity: timer.lan_kid1_s_internet_timer
- type: custom:mushroom-template-card
primary: >-
{% set t = 'timer.lan_jkid2_s_internet_timer' %}
{% set state = states(t) %}
{% set finish = state_attr(t, 'finishes_at') %}
{% set remain = state_attr(t, 'remaining') %}
{% set seconds = (as_timestamp(finish,0)-as_timestamp(now()))|int %}
{% set left = iif(finish == None,iif(remain == None,0,as_timedelta('0
' ~remain).seconds),seconds) %}
{{ left| timestamp_custom('%H:%M:%S', 0) }}
secondary: Time Remaining
icon: mdi:web
tap_action:
action: toggle
hold_action:
action: more-info
icon_color: ''
entity: timer.lan_kid2_s_internet_timer
columns: 2
- type: custom:slider-entity-row
entity: input_number.lan_kids_internet_timer_input
slider:
direction: left-right
background: solid
use_state_color: true
use_percentage_bg_opacity: false
show_track: false
toggle_on_click: false
force_square: false
show_name: true
show_state: true
compact: true
icon:
show: true
use_state_color: true
tap_action:
action: more-info
icon: ''
action_button:
mode: toggle
icon: mdi:power
show: true
show_spinner: true
tap_action:
action: toggle
name: Timer Length
columns: 1
What’s working is to press the button to call the script which starts the timer.
alias: LAN - Kid's Internet Timer Script
sequence:
- service: timer.start
data:
entity_id: "{{option}}"
duration: "{{ states('input_number.lan_kids_internet_timer_input') | int * 60 }}"
mode: single
I’m using an automation to toggle the switch which controls each device’s internet:
alias: LAN - Turn Kid's Internet on/off
description: ""
trigger:
- platform: state
entity_id:
- timer.lan_kid2_s_internet_timer
- timer.lan_kid1_s_internet_timer
condition: []
action:
- if:
- condition: state
entity_id: timer.lan_kid2_s_internet_timer
state: active
then:
- service: switch.turn_on
metadata: {}
data: {}
target:
entity_id: switch.kid2_s_chromebook
else:
- service: switch.turn_off
metadata: {}
data: {}
target:
entity_id: switch.kid2_s_chromebook
- if:
- condition: state
entity_id: timer.lan_kid1_s_internet_timer
state: active
then:
- service: switch.turn_on
metadata: {}
data: {}
target:
entity_id: switch.kid1_s_chromebook
else:
- service: switch.turn_off
metadata: {}
data: {}
target:
entity_id: switch.kid1_s_chromebook
mode: single
That all works to get the timer started. What I’d like to do is be able to hit the button on the dash a second time to cancel the timer which will turn off the internet (make the button more like a toggle).
When I try to add an if statement to the script using the variable "{{option}}"
which is from the button I get an error:
Here is the sript after including my IF statement:
alias: LAN - Kid's Internet Timer Script
sequence:
- service: timer.start
data:
entity_id: "{{option}}"
duration: "{{ states('input_number.lan_kids_internet_timer_input') | int * 60 }}"
enabled: false
- if:
- condition: state
entity_id: "{{option}}"
state: "on"
then:
- service: timer.cancel
data:
entity_id: "{{option}}"
else:
- service: timer.start
data:
entity_id: "{{option}}"
duration: >-
{{ states('input_number.lan_kids_internet_timer_input') | int * 60
}}
enabled: true
mode: single
Is there something about the IF statement that cannot take a variable the same as the service part?
Final thought → as I finished my final review of this I think I answered my long question when I realized that the variable is being passed as a part of the service call, hence why it is available to the service call, and not the IF statement.
Do I need to set an input_text
helper at the beginning of the script? I’ve gotta go help the kid’s with an outdoor project, but will test that later.