Hi, I’m getting very confused googling for HA scripting, then jinja, then YAML, then Templates, then back round again, then considering nodered and pyscript and appdaemon, then back round to scripting again… If there’s a good source that centralises using YAML, templating, jinja from a HA PoV I’d appreciate a link. As it stands I keep on seeing comments which end up like ‘Oh, you can’t use {{ }} in that setting, or Developer Templating doesn’t work that way in scripts’ or something else which means I’m just guessing what to try next.
My issue is I want a templating script like this:
{% set pct = (((faketemp /30 ) *100 ) | round(0) ) %}
{{ faketemp }}C = {{ pct }}%
{% if pct < 51 -%}
{% set bp = (((50 - pct)*2 ) | round(0) | int ) %}
{% set rp = 0 %}
{%- else -%}
{% set rp = ((( pct - 50)*2 ) | round(0) | int ) %}
{% set bp = 0 %}
{%- endif %}
{% set gp = (((100-(bp+rp))/4 )| round(0) | int ) %}
{% set MqttCmd = "Backlog led1 " ~ rp +"," ~ gp +"," ~ bp %}
{{ MqttCmd }}
to work in an HA script called by an automation. I have tried so many different ways of tweaking the script but keep on hitting silly errors like the ‘str/int’ problem - but with no clue where that problem is.
The idea of the script is that a temperature is converted to a percentage, and that is converted into RGB and sent to Tasmota to light up an led. I already have a working version that converts the temperature directly but I want a more generic program that converts a % so I can use similar code for other sensors.
The script I have (currently) is:
sequence:
- variables:
jinja_script: >
{% set Pct = ((((states('sensor.temp_e_temperature') /30 ) | int) *100 ) | round(0) | int ) %}
{% if ( Pct < 51) -%}
{% set LedR = 0 | int %}
{% set LedB = ( ( (50 - pct)*2 ) | round(0) | int ) %}
{%- else -%}
{% set LedB = 0 | int %}
{% set LedR = ( ( (pct - 50)*2 ) | round(0) | int ) %}
{%- endif %}
{% set LedG = (((100-(LedB+LedR))/4 )| round(0) | int ) %}
{% set MqttCmd = "Backlog led1 " ~ LedR + "," ~ LedG + "," ~ LedB %}
- service: notify.viaemail
data:
title: "Temp is: {{ states(\"sensor.temp_e_temperature\") }}"
message: "{{ MqttCmd }}"
target: ha@emailaddress
And the run error is
Error: TypeError: unsupported operand type(s) for /: ‘str’ and ‘int’
I think I’ve added ‘int’ everywhere I can
I’m keen to get to grips with this rather than add another integration but I have struggled with so many syntax problems and a lack of step-through debug that I really need some help.