I have a really long rule for the receiver. This rule is linked to a selection on the sitemap. Depending on the day/night time which is determined by the openhab_day switch the only thing which changes is actually the volume, some audyssey preseets and that’s about it. But to code it i have to write a really long rule and i know it can be simplified. So the question is how…
rule "OnkyoRules"
when Item onkyo_select_action received update
then
if (onkyo_select_action.state == 0) //Switch off
{
onkyoSource.sendCommand(5)
onkyoPower.sendCommand(OFF)
}
if (openhab_day == ON)
{
if (onkyo_select_action.state == 3) //Listen radio
{
onkyoPower.sendCommand(ON)
Thread::sleep(2000)
onkyoSource.sendCommand(43)
Thread::sleep(100)
onkyoVolume.sendCommand(47)
Thread::sleep(100)
onkyoAudysseyDynVol.sendCommand(0)
onkyoDimmerLevel.sendCommand(0)
Thread::sleep(100)
onkyoNETService.sendCommand(1)
onkyoNETPlay.sendCommand(ON)
}
if (onkyo_select_action.state == 4) //XBMC Day
{
onkyoPower.sendCommand(ON)
Thread::sleep(2000)
onkyoSource.sendCommand(1)
Thread::sleep(1000)
onkyoVolume.sendCommand(60)
onkyoAudysseyDynVol.sendCommand(0)
Thread::sleep(1000)
onkyoDimmerLevel.sendCommand(0)
}
if (onkyo_select_action.state == 6) //"PC Day"
{
onkyoPower.sendCommand(ON)
Thread::sleep(2000)
onkyoSource.sendCommand(5)
onkyoAudysseyDynVol.sendCommand(0)
onkyoDimmerLevel.sendCommand(0)
onkyoVolume.sendCommand(47)
}
}
if (openhab_day == OFF)
{
if (onkyo_select_action.state == 5) //XBMC at night
{
onkyoPower.sendCommand(ON)
Thread::sleep(2000)
onkyoSource.sendCommand(1)
Thread::sleep(1000)
onkyoVolume.sendCommand(35)
Thread::sleep(1000)
onkyoAudysseyDynVol.sendCommand(3)
onkyoDimmerLevel.sendCommand(2)
}
if (onkyo_select_action.state == 6) //"PC Day"
{
onkyoPower.sendCommand(ON)
Thread::sleep(2000)
onkyoSource.sendCommand(5)
onkyoAudysseyDynVol.sendCommand(0)
onkyoDimmerLevel.sendCommand(0)
onkyoVolume.sendCommand(37)
}
}
}
end
There are differences in the cases which YOU have to either confirm ( and keep to Kong code ) or change.
You are setting the Power then Sleep and setting the Source in all cases. The following Sleep differs in time, intentionally?In most cases the volume and the dimmer is set next, why not in all?
If you would follow the steps in all cases you could set the differing source,volume etc in variables and have the code to pass those settings to the items only once.
BTW You have 2 casee labeled as PC Day and no setting for Radio nigth.
This is a draft code so some mistakes could be there. Was looking for a general advise. So i shoul add a rule to set some variables depending on time of day like volume/Audyssey settings and just call on them from the main rule?
It doesn’t have to be a rule. The way @opus was describing I think he just meant defining a bunch of vals up front based on time of day and action and then having just the one block to send the commands out.
One thing you can do is swap your checks and check for state first and openhab_day second. This way all your your .state == 6 (for example) code would be together and you can have a short little if(openhab_day.state == ON) check for only those items that are different.
Another thing you can do which would not necessarily make the above shorter but it would make it somewhat simpler to read is to break each selection into a separate rule.
when Item onkyo_select_action received update 3
NOTE: I think that will work
Again, this will centralize your actions in one place and let you have short little sections which test for whether it is day or not.